#include "file_del.h"

//extern u8 idata device_active;
//extern DIR _xdata path_dj[8];
//extern FSAPIMSG _pdata fs_msg;
extern FATFS _xdata fat_fs;
extern u8 xdata vm_buffer[] ;
extern u8 _xdata id3_tag_buf[128];
extern u8 xdata id3_tag_len;

u8 delete_file()
{
    u8 delete=0;
    u8 timeout=0;


    u8 msg;
    fscan_init(vm_buffer);
    if (scan_fat.fs_type != FS_FAT16 && scan_fat.fs_type!=FS_FAT32)
    {
        return 0;
    }
    clear_lcd();
    ResShowMultiString( 0 , 0,cdelfile); // CENTER_ALIGN
    ResShowPic(DELLA , 0 , 0);

    show_file_name(0 , 2);

    while (1)
    {
        if (delete == 0)
            ResShowPic(CDEL_NO , 50 , 32);
        else
            ResShowPic(CDEL_YES , 50 , 32);
        draw_lcd(0,8);
        msg = app_get_msg();
        switch (msg)
        {
        case MSG_KEY_PREV:
        case MSG_KEY_NEXT:
            timeout = 0;
            delete = (delete==0)? 1 : 0;
            break;

        case MSG_KEY_MODE:
            if (delete)
            {
//                stop_decode();
                clear_lcd();
                ResShowPic(CHNDEL,0,16);
                draw_lcd(0,8);
                clear_dir();
                clear_fat_table();
                return 1;
            }
            return 0;
        case MSG_HALF_SECOND:
            if (timeout < MENUWAITTIME)
                timeout++;
            else
                return 0;
            break;
        case MSG_USB_DISK_OUT:
        case MSG_SDMMC_IN :
            given_device = DEVICE_SDMMC0;
            goto _EXIT_;
        case MSG_SDMMC_OUT:
        case MSG_USB_DISK_IN :
            given_device = DEVICE_UDISK;
            goto _EXIT_;
        default:
            break;

        }
    }
_EXIT_:
    given_file_method = PLAY_BREAK_POINT;
    put_msg_lifo(MSG_MUSIC_SELECT_NEW_DEVICE);
    return 0;
}

u8 delete_all_files()
{
    u8 delete=0;
    u8 timeout=0;
    u8 msg;
    fscan_init(vm_buffer);
    if (scan_fat.fs_type != FS_FAT16 && scan_fat.fs_type!=FS_FAT32)
    {
        return 0;
    }
    clear_lcd();
    ResShowMultiString( 0 , 0,cdelall);  // CENTER_ALIGN
    ResShowPic(DELLA , 0 , 0);
    while (1)
    {
        if (delete == 0)
            ResShowPic(CDEL_NO , 50 , 32);
        else
            ResShowPic(CDEL_YES , 50 , 32);
        draw_lcd(0,8);
        msg = app_get_msg();
        switch (msg)
        {
        case MSG_KEY_PREV:
        case MSG_KEY_NEXT:
            timeout = 0;
            delete = (delete==0)? 1 : 0;
            break;

        case MSG_KEY_MODE://case MSG_KEY_PP:
            if (delete)
            {
//                stop_decode();
                clear_lcd();
                ResShowPic(CHNDEL,0,16);
                draw_lcd(0,8);
                clear_all_file();
                return 1;
            }
            return 0;
        case MSG_HALF_SECOND:
            if (timeout < MENUWAITTIME)
                timeout++;
            else
                return 0;
            break;
        case MSG_USB_DISK_OUT:
        case MSG_SDMMC_IN :
            given_device = DEVICE_SDMMC0;
            goto _EXIT_;
        case MSG_SDMMC_OUT:
        case MSG_USB_DISK_IN :
            given_device = DEVICE_UDISK;
            goto _EXIT_;
        default:
            break;

        }
    }
_EXIT_:
    given_file_method = PLAY_BREAK_POINT;
    put_msg_lifo(MSG_MUSIC_SELECT_NEW_DEVICE);
    return 0;

}